Namespace('models.user',{
	init:function(){
		easyui.util.initDatagrid("#user",{
			url:contextPath+"/user/query",
			toolbar:"#toolbar",
			columns:[[
			 {field:'id',title:'id',width:80,checkbox:true},    
 			 {field:'username',title:'登录名',width:100},    
 			 {field:'nicename',title:'姓名',width:100}    
			]]
		});
	},
	query:function(){
		
	},
	add:function(){
		getDialog("adduser").dialog({
			href:contextPath+'/user/add',
			title: '新增',
		    width: 550,
		    height: 350,
		    buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					models.user.doAdd();
				}
				},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#adduser').dialog('destroy');
				}
			}]
		});
	},
	doAdd:function(){
		if(!$("#userform").form('validate')){
			return;
		}
		var user = $("#userform").serialize();
		$.post(contextPath+"/user/add",user,function(data){
			if(data){
				alert("新增用户成功！");
				$('#adduser').dialog('destroy');
				$("#user").datagrid('load');
			}else{
					alert("新增用户失败！");
			}
		});
	},update:function(){
		
		var row =$("#user").datagrid('getSelected');
		if(row.length==0){
			alert("请选择一条数据！");
			return;
		}
		getDialog("updateuser").dialog({
			href:contextPath+'/user/update?id='+row.id,
			title: '新增',
		    width: 550,
		    height: 350,
		    buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					models.user.doUpdate();
				}
				},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#updateuser').dialog('destroy');
				}
			}]
		});
	},doUpdate:function(){
		var user = $("#userform").serialize();
		$.post(contextPath+"/user/update",user,function(data){
			if(data){
				alert("修改用户成功！");
				$('#updateuser').dialog('destroy');
				$("#user").datagrid('load');
			}else{
					alert("修改用户失败！");
			}
		});
	},
	remove:function(){
		var row =$("#user").datagrid('getSelected');
		if(row.length==0){
			alert("请选择一条数据！");
			return;
		}
		$.post(contextPath+"/user/remove",{id:row.id},function(data){
			if(data){
				$("#user").datagrid('load');
			}else{
					alert("修改用户失败！");
			}
		});
	}
	
});
$(function(){
	models.user.init();
});